package com.doule.farmerassistant.plan;

import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.doule.farmerassistant.R;
import com.doule.farmerassistant.TasksCheckAdapter;
import com.doule.farmerassistant.task.TaskDetail;

import java.util.ArrayList;

/**
 * ShowTaskDialog class.
 * 
 * @author riguang
 * 
 */
public class ShowTaskDialog implements OnClickListener, OnItemClickListener {

    /**
     * Task listView.
     */
    private ListView mTaskListView;

    /**
     * TaskDetail table.
     */
    private ArrayList<TaskDetail> mTaskList;

    /**
     * TasksCheckAdapter.
     */
    private TasksCheckAdapter mTaskAdapter;

    /**
     * CreatePlanActivity.
     */
    private CreatePlanActivity mContext;

    /**
     * Show task dialog.
     */
    private Dialog mShowTaskDialog;

    /**
     * Temp TaskDetail table.
     */
    private ArrayList<TaskDetail> temporaryList;

    /**
     * Constructor.
     * 
     * @param context
     *            CreatePlanActivity
     */
    public ShowTaskDialog(CreatePlanActivity context) {
        this.mContext = context;
        showDialog();
    }

    /**
     * Show dialog.
     */
    private void showDialog() {
        temporaryList = new ArrayList<TaskDetail>();
        mShowTaskDialog = new Dialog(mContext, R.style.Theme_dialog1);
        mShowTaskDialog.setContentView(R.layout.dialog_show_task);
        mShowTaskDialog.getWindow().setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mTaskListView = (ListView) mShowTaskDialog
                .findViewById(R.id.lv_show_task);
        //mTaskList = TasksSqlHelper.loadTasks();
        mTaskAdapter = new TasksCheckAdapter(mContext, mTaskList);
        mTaskListView.setAdapter(mTaskAdapter);
        mTaskListView.setOnItemClickListener(this);
        mShowTaskDialog.findViewById(R.id.btn_add_task)
                .setOnClickListener(this);
        mShowTaskDialog.findViewById(R.id.btn_delete_task).setOnClickListener(
                this);
        mShowTaskDialog.show();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == R.id.btn_add_task) {
            for (TaskDetail data : mTaskList) {
                if (data.isChecked()) {
                    temporaryList.add(data);
                }
            }
        }
        mContext.showTasks(temporaryList);
        if (mShowTaskDialog != null) {
            mShowTaskDialog.dismiss();
            mShowTaskDialog = null;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        // TODO Auto-generated method stub
        String type = mTaskList.get(position).getType();
        String id = mTaskList.get(position).getId();
        String name = mTaskList.get(position).getName();
        String batchType = mTaskList.get(position).getPerbatch();
        String duration = mTaskList.get(position).getDuration();
        Log.i("mytest", "List noti is : " + type + " ; " + id + " ; " + name
                + " ; " + batchType + " ; " + duration);
    }
}
